package com.edu.hre.db.sql.recruit;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.common.utils.StringConstants;
import com.edu.hre.db.pojo.HreCandidateLanguage;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.recruit.CanLanguageForm;
import com.edu.hre.struts.form.recruit.CanLanguageListForm;

public class CanLanguageDAOService implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3394301416150421253L;
	public static final String BEAN_NAME = "canLanguageDAOService";
	private BaseService baseService  = null;
	 
	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	} 

	public ArrayList<CanLanguageForm> getCanLanguageList(int limit, int offset, int canId){

		ArrayList<CanLanguageForm> listResult = new ArrayList<CanLanguageForm>();

		String sql = "SELECT clang.id AS col1," +
					" lang.name AS col2," +
					" clang.read_competency AS col3," +
					" clang.speak_competency AS col4," +
					" clang.listen_competency AS col5," +
					" clang.write_competency AS col6" +

					" FROM hre_candidate_language AS clang" +
					" LEFT OUTER JOIN hre_language AS lang ON clang.language_id = lang.id" +
					" LEFT OUTER JOIN hre_job_candidate AS can ON clang.candidate_id = can.id " +

					" WHERE can.is_deleted = 0 " +
					" AND can.id = ? " +
					" ORDER BY clang.date_created DESC";

		List <Object[]> listObject = baseService.listArrayObject(sql, limit, offset, canId);
		for(int i = 0; listObject != null && i < listObject.size(); i++){

			try {

				Object[] objs = listObject.get(i);
				CanLanguageForm language = new CanLanguageForm();
	
				language.setId(Integer.parseInt(CommonUtils.normalizeObjectToString(objs[1], "0")));
				language.setCanId(canId);
				language.setLanguage(CommonUtils.normalizeObjectToString(objs[2], ""));
				int read = Integer.parseInt(CommonUtils.normalizeObjectToString(objs[3], "0"));
				int speak = Integer.parseInt(CommonUtils.normalizeObjectToString(objs[4], "0"));
				int listen = Integer.parseInt(CommonUtils.normalizeObjectToString(objs[5], "0"));
				int write = Integer.parseInt(CommonUtils.normalizeObjectToString(objs[6], "0"));
				
				if (read < 0) {
					read = 0;
				}
				if (read > 10) {
					read = 10;
				}
				
				if (speak < 0) {
					speak = 0;
				}
				if (speak > 10) {
					speak = 10;
				}
				
				if (listen < 0) {
					listen = 0;
				}
				if (listen > 10) {
					listen = 10;
				}
				
				if (write < 0) {
					write = 0;
				}
				if (write > 10) {
					write = 10;
				}
				
				int total = (read + speak + write + listen) / 4;
				
				language.setReadCompetency(read + " / 10 ["  + StringConstants.ALL_COMPETENCIES[read] + "]");
				language.setSpeakCompetency(speak + " / 10 ["  + StringConstants.ALL_COMPETENCIES[speak] + "]");
				language.setListenCompetency(listen + " / 10 ["  + StringConstants.ALL_COMPETENCIES[listen] + "]");
				language.setWriteCompetency(write + " / 10 ["  + StringConstants.ALL_COMPETENCIES[write] + "]");
				language.setGeneralCompetency(total + " / 10 ["  + StringConstants.ALL_COMPETENCIES[total] + "]");
	
				listResult.add(language);
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}

		return listResult;
	}

	public int countCanLanguages(int canId){

		int count = baseService.countByColumns(new String[]{"candidateId"}, new int[]{Compare.EQUAL}, new Integer[]{canId}, "id", HreCandidateLanguage.class);
		return count < 0 ? 0 : count;
	}

	public void deleteLanguages(CanLanguageListForm form){

		ArrayList<CanLanguageForm> info = (ArrayList<CanLanguageForm>) form.getAllLanguages();

		for (int i = 0; i < info.size(); i++) {
			CanLanguageForm lang = info.get(i);
			if (lang.isSelected()) {
				baseService.deleteFirstPojo("id", Compare.EQUAL, lang.getId(), null, null, 0, HreCandidateLanguage.class);
			}
		}
	}
}
